﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Super_Pong.BaseClasses;
using Microsoft.Xna.Framework.Input;

/*
 * AVISO:
 * ESSA CLASSE NAO ESTÁ TERMINADA! NÃO MECHA SEM A AUTORIZAÇÃO DO AUTOR!
*/

/*
 * AVISO:
 * MUDANÇA DE AUTOR, EU LUCAS CARVALHAES ASSUMI ESSA CLASSE!
 * nem lembrava disso...
*/

//TODO : Add mouse position tracking

namespace Super_Pong.Classes
{
    public enum gunType
    {
        BLOCK,
        LASER,
        PHOTON
    }

    public class Weapon : BarPiece
    {
        #region Attributes
        private string name;
        private List<Bullet> bullets;
        private gunType type;
        private int damage;
        private Animation animation;
        private float relativePosition;
        private Vector2 position;
        private float rotation;
        private Bar barTracker;
        private GameWindow gWindowTracker;
        private MouseState mouse;
        private MouseState mouseOld;
        private string weaponSide;
        private Vector2 bulletOrigin;

        Animation bulletAnim, hitAnim;
        #endregion

        #region Methods

        //Constructor
        /// <summary>
        /// Complete constructor.
        /// </summary>
        /// <param name="gWindow">Game window</param>
        /// <param name="barra">The bar that welds this gun</param>
        /// <param name="anim">The weapon look.</param>
        /// <param name="name">The weapon name.</param>
        /// <param name="position">Its position RELATIVE TO THE BAR. (0 top , 1 bottom)</param>
        /// <param name="type">The weapon type.</param>
        /// <param name="price">The weapon price.</param>
        /// <param name="damage">The gun avarage damage.</param>
        /// <param name="bulletAnim">The bullet look.</param>
        /// <param name="hitAnim">The bullet hit animation.</param>
        /// <param name="side">the side. put "LEFT" or "RIGHT" to choose.</param>
        public Weapon(GameWindow gwindow, Bar barra, Animation anim, string name, float position, gunType type, int price, int damage, Animation bulletAnim, Animation hitAnim, string side, Vector2 bulletCenter)
        {
            gWindowTracker = gwindow;
            barTracker = barra;
            animation = anim;
            this.name = name;
            relativePosition = position;
            this.type = type;
            this.price = price;
            bullets = new List<Bullet>();
            this.damage = damage;
            this.bulletAnim = bulletAnim;
            this.hitAnim = hitAnim;
            this.rotation = 0;
            weaponSide = side;
            bulletOrigin = bulletCenter;
        }

        #region gets
        public gunType GetType()
        {
            return this.type;
        }

        public string GetName()
        {
            return this.name;
        }
        #endregion


        /// <summary>
        /// Activates when the player shoots with a weapon.
        /// </summary>
        /// <param name="target">the position the player chose.</param>
        public void Shoot(Vector2 target)
        {
            //TODO : add some randomicity here
            bullets.Add(new Bullet(damage, bulletAnim, hitAnim, this.GetType(), position, Vector2.Normalize(target - position), gWindowTracker, rotation, bulletOrigin));
        }


        /// <summary>
        /// Get angle
        /// </summary>
        /// <param name="mousePos">mouse position</param>
        /// <returns></returns>
        public float setWpnDirection(Vector2 mousePos)
        {
            rotation = (float)Math.Atan2((double)(mousePos.Y - this.position.X), (double)(mousePos.X - this.position.X));
            return rotation;
        }


        /// <summary>
        /// Update the weapon.
        /// </summary>
        /// <param name="gameTime">gametime.</param>
        public override void Update(GameTime gameTime)
        {
            //Update mouse position and weapon rotation
            mouse = Mouse.GetState();
            Vector2 mousePos = new Vector2(mouse.X, mouse.Y);
            setWpnDirection(mousePos);

            //Shooting code
            if (weaponSide == "LEFT")
            {
                if (mouse.LeftButton == ButtonState.Pressed)
                {
                    Shoot(mousePos);
                }
            }
            if (weaponSide == "RIGHT")
            {
                if (mouse.RightButton == ButtonState.Pressed)
                {
                    Shoot(mousePos);
                }
            }

            //Update the weapon anim
            animation.Update(gameTime);

            //Update the weapon position
            position.X = barTracker.getPosition().X + barTracker.getWidth()/2;
            position.Y = barTracker.getPosition().Y * relativePosition - this.animation.GetCurrentImage().Height/2;

            //Update all bullets
            foreach (Bullet b in bullets)
            {
                b.Update(gameTime);
            }

            //Clear the bullet list
            bullets.RemoveAll(k => k == null);

            //Assign old mouse state to mouseOld
            mouseOld = mouse;
        }

        /// <summary>
        /// Draw the weapon. To be edited later.
        /// </summary>
        /// <param name="spriteBatch">spritebatch...</param>
        public override void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Begin();
            //Draw the weapon
            spriteBatch.Draw(
                animation.GetCurrentImage(),
                position,
                null,
                Color.White,
                rotation,
                new Vector2(animation.GetCurrentImage().Width/2,animation.GetCurrentImage().Height/2),
                1.0f,
                SpriteEffects.None,
                0.0f
                );
            spriteBatch.End();
        }

        public override void collide(Entity e)
        {
            throw new NotImplementedException();
        }
        #endregion
    }
}
